$(function(){
    //FansyBox elements
    var fancyOptions = {frameWidth:500, padding:0};
    var fancyFrameOptions = {type:'iframe', maxWidth:600, maxHeight:600, padding:10};

    $("a.fancy").fancybox(fancyOptions);
    $("a.frame").fancybox(fancyFrameOptions);

    //Main thumb click
    $('#main-thumb-ref').click(function(){
        $('#tab-photos').click();

        return false;
    });

    //Write review click
    $('#write-review').click(function(){
        $('#tab-reviews').click();

        return false;
    });

    //Simple tabs
    function isTab(el_id) {
        var v = el_id.split('-');
        try {
            if (v[0] == 'tab') {
                var c = $('#tab-' + v[1] + '-content').get(0);
                //first time only
                var a = $('#simple-tabs').children('li').filter(':first').children('a').get(0);
                var firstv = a.id.split('-');
                if (!isTab.inittab_in_container) {
                   isTab.inittab_in_container = true;
                   $('#tab-' + firstv[1] + '-content').html($('#simple-tabs-body').html());
                }
                return c != null;
            } else {
                return false;
            }
        } catch(e) {}
    }
    isTab.inittab_in_container = false;

    $('#simple-tabs a').click(function(e){
        is_tab = isTab(e.target.id);
        if (is_tab != true) {
            return true;
        }

        //Disactivate all tabs
        $('#simple-tabs').children('li').each(function() {
            try {
                var cl = $(this).attr('class');
                var cl_words = cl.split('_');
                $(this).attr('class', cl_words[0] + '_' + cl_words[1] +  '_' +  cl_words[2] + '_off');
                var link = $(this).children('a[id^=tab]').get(0);
                $(link).removeAttr('class');
            }catch(e){
                alert(e.message);
            }
        });

        //Activate only clicked tab
        $(e.target).attr('class', 'hotel_tab_active');
        var tab_li_class = $(e.target).closest('li').attr('class');
        var li = tab_li_class.split('_');
        $(e.target).closest('li').attr('class', li[0] + '_' + li[1] +  '_' +  li[2] + '_on');

        //Set content
        var container = e.target.id + '-content';
        $('#simple-tabs-body').html($('#' + container).html());

        //activate fansybox
        $("a.fancy").fancybox(fancyOptions);
        $("a.frame").fancybox(fancyFrameOptions);

        return false;
    });


});